import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:test_flutter/util/dialog_utils.dart';
import 'package:test_flutter/util/permission_utils.dart';
import 'package:toggle_switch/toggle_switch.dart';
import 'package:webview_flutter/webview_flutter.dart';

class UIPage extends StatefulWidget {
  @override
  // State<StatefulWidget> createState() => _ToastState();
  // State<StatefulWidget> createState() => _PermissionState();
  State<StatefulWidget> createState() => _WebViewState();
}

///加载webView
class _WebViewState extends State<UIPage> {
  Completer<WebViewController> _controller = Completer<WebViewController>();
  late WebViewController _webViewController;

  @override
  void initState() {
    super.initState();
    // if(Platform.isAndroid) {
    //   WebView.platform = SurfaceAndroidWebView();
    // }
    Future(() async {
      await _initController();
    });
  }

  Future<void> _initController() async {
    await _controller.future.then((controller) {
      _webViewController = controller;
      _webViewController.loadRequest(Uri.parse('https://cn.bing.com/'));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('加载WebView'),
      ),
      body: Container(
        alignment: Alignment.center,
        child: WebViewWidget(
          controller: _webViewController,
        ),
      ),
    );
  }
}

///权限管理
class _PermissionState extends State<UIPage> {
  bool status = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('权限管理'),
      ),
      body: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              onPressed: () async {
                PermissionUtils.checkStorage();
              },
              child: Text('检查存储权限'),
            ),
            TextButton(
              onPressed: () async {
                PermissionUtils.requestStorage();
              },
              child: Text('请求存储权限'),
            ),
            TextButton(
              onPressed: () async {
                PermissionUtils.checkCamera();
              },
              child: Text('检查相机和联系人权限'),
            ),
            TextButton(
              onPressed: () async {
                PermissionUtils.requestCamera();
              },
              child: Text('请求相机和联系人权限'),
            ),
            TextButton(
              onPressed: () async {
                DialogUtils.showDialog(context);
              },
              child: Text('提示框'),
            ),
            _flutterSwitch(),
            _toggleSwitch(),
          ],
        ),
      ),
    );
  }

  _toggleSwitch() {
    return ToggleSwitch(
      minWidth: 150.0,
      labels: ['李枚', '李超', '邹宙倍'],
      initialLabelIndex: 0,
      onToggle: (index) {
        print('select: $index');
      },
      icons: [Icons.favorite, Icons.favorite, Icons.accessibility],
      cornerRadius: 20.0,
      inactiveBgColor: Colors.grey,
      inactiveFgColor: Colors.white,
      activeFgColor: Colors.red,
    );
  }

  ///flutter开关
  _flutterSwitch() {
    return FlutterSwitch(
      width: 125.0,
      height: 55.0,
      valueFontSize: 25.0,
      toggleSize: 45.0,
      borderRadius: 30.0,
      padding: 8.0,
      showOnOff: true,
      value: status,
      onToggle: (value) {
        print('switch value:$value');
        setState(() {
          status = value;
        });
      },
    );
  }
}

///Toast提示
class _ToastState extends State<UIPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Toast提示'),
      ),
      body: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              onPressed: () {
                Fluttertoast.showToast(
                    msg: '网络异常：-1',
                    toastLength: Toast.LENGTH_SHORT,
                    gravity: ToastGravity.CENTER,
                    timeInSecForIosWeb: 1,
                    backgroundColor: Colors.red,
                    textColor: Colors.white,
                    fontSize: 16.0);
              },
              child: Text('请求1'),
            ),
            TextButton(
              onPressed: () {
                Fluttertoast.showToast(
                    msg: '请求成功',
                    toastLength: Toast.LENGTH_SHORT,
                    gravity: ToastGravity.BOTTOM,
                    timeInSecForIosWeb: 1,
                    backgroundColor: Colors.black12,
                    textColor: Colors.black,
                    fontSize: 16.0);
              },
              child: Text('请求2'),
            ),
            TextButton(
              onPressed: () {
                Fluttertoast.cancel();
              },
              child: Text('取消请求'),
            ),
          ],
        ),
      ),
    );
  }
}
